//
//  PKRadioDetailViewController.m
//  PianKe
//
//  Created by lanou on 15/12/23.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKRadioDetailViewController.h"
#import "PKDetailModel.h"
#import "PKDetailTableViewCell.h"
#import "PKRadioDetailHeadView.h"
#import "PKPlayMainViewController.h"

@interface PKRadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>


// tableView展示
@property (strong, nonatomic) UITableView *tableView;
// 展示cell上的数据
@property (strong, nonatomic) NSMutableArray *dataArray;
// radioInfo的model
@property (strong, nonatomic) PKRadioModel *radioInfoModel;


// 保存topImageV的图片
@property (strong, nonatomic) NSString *topImageURL;

// 表头
@property (strong, nonatomic) PKRadioDetailHeadView *headView;

@property (strong, nonatomic) UILabel *titleLabel;


// 加载数据的初始位置
@property (assign, nonatomic) NSInteger requestStart;



@end

@implementation PKRadioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 关闭系统自动修改我们的视图
     self.automaticallyAdjustsScrollViewInsets = NO;

     self.view.backgroundColor = [UIColor orangeColor];
    
    self.dataArray = [NSMutableArray array];
    
    _requestStart = 0;
    
    self.radioInfoModel = [[PKRadioModel alloc] init];
    
      [self addTableView];
    
     [self setUpDatailData];
    
    [self addBarButtonItem];
    
    
    
#pragma mark ----- 上啦加载
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        _requestStart += 10;
        
        [self addListData];
        
    }];
    
    
#pragma mark ----- 下拉刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        _requestStart = 0;
        
        [self setUpDatailData];
        
    }];
    
    
    
    
    
    
    
}

- (void)addListData
{
    
    
    // 创建一个字典
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"CMBvEXi1pvhDCsN9GMPukDJxQMSsMm2KLp2lkWjc2cWP22N1OSxte7vs";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"version"] = @"3.0.6";
    parDic[@"radioid"] = self.radioid;
    parDic[@"limit"] = @"10";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_requestStart];
    
    
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/radio_detail_list" parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        NSArray *array = dataDic[@"list"];
        for (NSDictionary *oneDic in array) {
            
            PKDetailModel *model = [[PKDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [self.dataArray addObject:model];
            
        }
        
#pragma mark ----- 结束上啦加载
        
        [_tableView.mj_footer endRefreshing];
        
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        
    }];

    
    
}







// 添加一个返回导航栏
- (void)addBarButtonItem
{
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:headView];
    
    UIButton *leftBack = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBack.frame = CGRectMake(10, 30, 30, 30);
    [leftBack setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [headView addSubview:leftBack];
    [leftBack addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftBack.right + 10, leftBack.top, 200, leftBack.height)];
    [headView addSubview:self.titleLabel];
       
    
    
}

// 实现返回
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

// 处理数据
- (void) setUpDatailData
{
    // 创建一个字典
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"CMBvEXi1pvhDCsN9GMPukDJxQMSsMm2KLp2lkWjc2cWP22N1OSxte7vs";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"F7EFB566-54F1-4912-ADFC-7E788F6D4D00";
    parDic[@"version"] = @"3.0.6";
    parDic[@"radioid"] = self.radioid;
   
   
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/radio_detail" parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *dataDic = dic[@"data"];
        NSArray *array = dataDic[@"list"];
        
        [_dataArray removeAllObjects];
        
        for (NSDictionary *oneDic in array) {
            
            PKDetailModel *model = [[PKDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [self.dataArray addObject:model];
            
        }
        
#pragma mark ---- 取出topimage radioInfo信息
        NSDictionary *radioinfoDic = dataDic[@"radioInfo"];
      //  self.topImageURL = radioinfoDic[@"coverimg"];
        [self.radioInfoModel setValuesForKeysWithDictionary:radioinfoDic];
        self.titleLabel.text = self.radioInfoModel.title;
        
        
#pragma mark ---- 结束下拉刷新
        [_tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
   
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        
    }];
    
    
}


// 添加表
- (void)addTableView
{
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    
    // 设置表头
    self.headView = [[PKRadioDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 300)];
    self.tableView.tableHeaderView = self.headView;

    [self.view addSubview:self.tableView];
    
    
    
}

// tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"DetailCell";
    PKDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[PKDetailTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
        
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    PKDetailModel *model = self.dataArray[indexPath.row];
    // 展示数据
    cell.model = model;
    
    if (indexPath.row == 0) {
        
        self.headView.radioInfoModel = self.radioInfoModel;
       
    }
    
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PKPlayMainViewController *mainVC = [[PKPlayMainViewController alloc] init];

    mainVC.listArray = self.dataArray;
    
    // 告诉后面的页面,我点击的是哪个电台
    mainVC.firstIndex = indexPath.row;
    
    
    [self.navigationController pushViewController:mainVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
